class Admin::BlockController < Admin::BaseAdminController
  
  def list
    @blocks = @blog.blocks.paginate :per_page => @@PER_PAGE, :page => params[:page],
      :order => :position
  end

  def create
    @block = Block.new(params[:block])
    if request.post? && @blog.blocks << @block
      redirect_to_default(@block.name + " saved.")
    end
  end
  
  def preview
    if request.xhr?
      render :text => params[:block][:content]
    end
  end

  def update
    @block = Block.find(params[:id])
    if request.post? && @block.update_attributes(params[:block])
      redirect_to_default(@block.name + " updated.")
    end
  end

  def delete
    if request.post?
      block = Block.find(params[:id])
      if block.destroy
        redirect_to_default(block.name + " deleted")
      end
    end
  end
  
  def move_up
    if request.post?
      block = Block.find(params[:id])
      block.move_higher
      redirect_to_default(block.name + " moved up.")
    end
  end
  
  def move_down
    if request.post?
      block = Block.find(params[:id])
      block.move_lower
      redirect_to_default(block.name + " moved down.")
    end
  end
  
  private 
  def redirect_to_default(notice = nil)
    flash[:notice] = notice unless notice.nil?
    redirect_to(:action => :list)
  end
end
